import _config from "./config.js"
import cos from './tx-sdk.js'
import store from './store.js'
import request from './interface'
import https from 'https/request.js'
export default {
	userInfo: {},
	address: {},
	statetype:0,
	// #ifdef APP-PLUS
	source: 1,
	// #endif
	// #ifdef MP-WEIXIN
	source: 3,
	// #endif
	back(delta = 1) {
		uni.navigateBack({
			delta: delta
		})
	},
	phoneFun(phones){
	    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
	    if (!myreg.test(phones)) {
	      return false;
	    } else {
	      return true;
	    }
	},
	// 保存用户登录信息
	async getLoginInfo (){
		// 控制显示隐藏 方便过百度小程序审核
		request({
			url:"Notice/notice_getone",
			data:{
				id:5
			}
		}).then(res => {
			if(res.data.create_time=='2020-11-13 20:06:45'){
				this.statetype = 1; 
			}else if(res.data.create_time=='2020-11-13 20:06:46'){
				this.statetype = 2;
			}else{
				this.statetype = 0;
			}
		})
		
		let loginInfo = await uni.getStorageSync('loginInfo')
		if (loginInfo) {
			_config.loginInfo = loginInfo
			this.userInfo = loginInfo.userInfo
			this.userInfo.timId = loginInfo.timId
			// console.log(this.userInfo);
			await Promise.all([
				store.commit('saveUserInfo',loginInfo.userInfo),
				/* 初始化 im */
				// cos.tim_init(loginInfo.timId,loginInfo.usersig),
				this.userInfo.exam_type == 2 ? store.dispatch('setTabBar') : {}
			])

			return loginInfo
		} else {
			_config.loginInfo = false;
		}
	},
	// 微信支付
	wxpay(data) {
		return new Promise((resolve,reject) => {
			uni.requestPayment({
			    provider: 'wxpay',
			    // #ifdef APP-PLUS
			    orderInfo: data,
			    // #endif
				// #ifdef MP-WEIXIN
				timeStamp: data.timestamp,
				nonceStr: data.nonceStr,
				package: data.package,
				signType: data.signType,
				paySign: data.paySign,
				// #endif
			    success: function (res) {
			        console.log('success:' + JSON.stringify(res));
					resolve(true)
			    },
			    fail: function (err) {
			        console.log('fail:' + JSON.stringify(err));
					reject(false)
			    }
			});
		})
	},
	// 未开通VIP提示
	not_VIP_pop() {
		uni.showModal({
			title: '温馨提示',
			content: '您未开通VIP,开通VIP可享更多特权!',
			cancelText: '暂不开通',
			confirmText: '立即开通',
			success: function (res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages_a/my/vip/member'
					})
					uni.hideToast();
				} else if (res.cancel) {
					uni.navigateBack()
					uni.hideToast();
				}
			}
		})
	},
	not_login_show: true,
	// 未登录提示
	not_login_pop() {
		uni.showModal({
			title: '温馨提示',
			content: '检测到您还未登录，是否前往登录',
			cancelText: '暂不',
			confirmText: '登录',
			success: (res => {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages_a/login/index'
					})
					uni.hideToast();
				} else if (res.cancel) {
					uni.navigateBack()
					uni.hideToast();
				}
				this.not_login_show = true
			})
		})
	},
	async jump(url, query) {
		// #ifndef APP-PLUS
		if (url == '/pages/art_probability/index') url = '/pages/wish/index'
		// #endif
		uni.navigateTo({
			url: url,
			fail: (err => {
				if (query) uni.$emit('switch', query)
				uni.switchTab({
					url: url,
					fail(err) {
						console.log(err);
					}
				})
			})
		})
	},
	// 安装新版本
	check_updates(opt) {
		let _this = this
		if (plus.runtime.version != opt.version) {
			// android 安装包地址
			uni.getStorage({
				key: 'install_package',
				success (path){
					uni.getSystemInfo({
					    success: function (res) {
							switch (res.platform){
								case 'android':
									_this.isUpdate(path.data,opt.android)
									break;
								case 'ios':
								// ios 安装包地址
									_this.isUpdate(path.data,opt.ios)
									break;
								default:
									break;
							}
					    }
					});
				},
				fail() {
					uni.getSystemInfo({
					    success: function (res) {
							switch (res.platform){
								case 'android':
									_this.download_pack(opt.android)
									break;
								case 'ios':
								// ios 安装包地址
									_this.download_pack(opt.ios)
									break;
								default:
									break;
							}
					    }
					});
				}
			})
		}
	},
	// 下载安装包
	download_pack(url) {
		let _this = this
		const down = uni.downloadFile({
			url: url, //仅为示例，并非真实的资源
			success: (res) => {
				if (res.statusCode === 200) {
					uni.saveFile({
						tempFilePath: res.tempFilePath,
						success(data){
							uni.setStorage({
								key: 'install_package',
								data: data.savedFilePath
							})
						}
					})

					_this.isUpdate(res.tempFilePath)
				}
			}
		});
	},
	// 提示用户更新
	isUpdate(path,url) {
		uni.showModal({
			title: '提示',
			content: '检测到有新版本，是否安装',
			success: function (res) {
				if (res.confirm) {
					plus.io.getFileInfo({
						filePath: path,
						success: function(res){
							// 安装
							plus.runtime.install(
								path, {
									force: false,
								}, e => {
									// console.log(e);
								}, err => {
									// console.log('安装失败')
								}
							);
						},
						fail (){
							uni.removeStorage({key: 'install_package'});
							download_pack(url)
						}
					})

				} else if (res.cancel) {
					uni.setStorage({
						key: 'noUpdate',
						data: true
					})
				}
			}
		});
	},
	pagination(arr, arr1, last_page, current_page = 1, status = 'more') {
		let data = {}
		// console.log(last_page,current_page);
		if(current_page > last_page) {
			data = {page: current_page,status: 'noMore'}
		} else if (current_page == last_page) {
			data = {list: arr1,page: current_page+=1,status: 'noMore'}
		} else {
			if (arr.length) {
			// console.log(1);
				data.list = arr.concat(arr1)
			} else {
			// console.log(2);
				data.list = arr1
			}
			data.page = current_page+=1
			data.status = status
		}
		return data
	}
}
